package org.shipp.util;

import java.util.LinkedList;

import org.shipp.model.Item;
import org.shipp.model.XMLParser;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
 * 
 * @author Efren
 *
 */
public class MenuConfigAdapter {
	public static LinkedList<Item> getMenuDefault() {
		LinkedList<Item> ret = new LinkedList<Item>();
		String xml = Item.dd;

		XMLParser parser = new XMLParser();
		Document doc = parser.getDomElement(xml);

		NodeList nlMenu = doc.getChildNodes();// nos dentro de <menu>
		for (int i = 0; i < nlMenu.getLength(); i++) {
			if (nlMenu.item(i).getNodeType() == Node.ELEMENT_NODE) {
				Element eMenu = (Element) nlMenu.item(i);

				NodeList nlItem = eMenu.getChildNodes();// nos dentro de <item>
				eMenu.getNodeType();
				for (int i2 = 0; i2 < nlItem.getLength(); i2++) {
					if (nlItem.item(i2).getNodeType() == Node.ELEMENT_NODE) {
						Element eItem = (Element) nlItem.item(i2);

						Item item = new Item();
						LinkedList<Item> items = new LinkedList<Item>();

						NodeList nlItems = eItem.getElementsByTagName("items");// nos dentro de <items>
						for (int i3 = 0; i3 < nlItems.getLength(); i3++) {
							if (nlItems.item(i3).getNodeType() == Node.ELEMENT_NODE) {
								Element eItems = (Element) nlItems.item(i3);

								NodeList nlItems2 = eItems.getElementsByTagName("item");// nos dentro  de <item>
								for (int i4 = 0; i4 < nlItems2.getLength(); i4++) {
									if (nlItems2.item(i4).getNodeType() == Node.ELEMENT_NODE) {
										Element eItem2 = (Element) nlItems2.item(i4);
										Item itemInterno = new Item();
										itemInterno.setLink(parser.getValue(eItem2, "link"));
										itemInterno.setTitulo(parser.getValue(eItem2, "titulo"));
										items.add(itemInterno);
									}
								}
							}
						}

						item.setItems(items);
						item.setTitulo(parser.getValue(eItem, "titulo"));
						ret.add(item);
					}
				}
			}
		}

		return ret;
	}
}
